<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>UDP协议 | 知识库</title>
    <meta name="generator" content="VuePress 1.9.2">
    <link rel="icon" href="/vuepress-theme-vdoing-doc/img/favicon.ico">
    <noscript><meta http-equiv="refresh" content="0; url=https://www.youngkbt.cn/noscript/"><style>.theme-vdoing-content { display:none }</noscript>
    <meta name="description" content="一个基于VuePress的 知识管理">
    <meta name="keywords" content="vuepress,theme,blog,vdoing">
    <meta name="theme-color" content="#11a8cd">
    
    <link rel="preload" href="/vuepress-theme-vdoing-doc/assets/css/0.styles.b0974950.css" as="style"><link rel="preload" href="/vuepress-theme-vdoing-doc/assets/js/app.f061d58b.js" as="script"><link rel="preload" href="/vuepress-theme-vdoing-doc/assets/js/2.9bf30e88.js" as="script"><link rel="preload" href="/vuepress-theme-vdoing-doc/assets/js/83.460cd49b.js" as="script"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/10.d2937c56.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/11.67567365.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/12.74e8c324.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/13.2376fcd7.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/14.57ef5c33.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/15.44e7c0ba.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/16.c2fb4c44.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/17.af212389.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/18.1e6acac7.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/19.54653ac9.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/20.920cc499.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/21.37128fe3.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/22.8f68a2f2.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/23.7b9f1ba6.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/24.12adc291.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/25.cfe381e5.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/26.9cf879f6.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/27.4e6190ea.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/28.1e22fc79.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/29.c57e047d.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/3.cdcce428.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/30.21a95474.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/31.ce97e6e3.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/32.cdb45d73.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/33.fd95e780.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/34.cd49a408.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/35.fe128161.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/36.02043ece.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/37.02eee249.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/38.4650dec6.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/39.04fb7652.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/4.c02b1e5f.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/40.7b51f807.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/41.1df513a9.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/42.92a82cec.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/43.363fceef.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/44.867b24fd.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/45.89f64bfa.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/46.3a4c53d8.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/47.b8a31c80.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/48.7a584b38.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/49.98345029.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/5.cb45b2c7.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/50.6a0b68f4.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/51.8945cdf6.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/52.6583e327.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/53.f6987dfc.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/54.cca012f3.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/55.8c58cf5c.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/56.23e08364.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/57.08b54de0.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/58.131ee23e.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/59.45637607.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/6.c97c5dc9.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/60.fe9c5dd1.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/61.39ad8fda.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/62.ede47d34.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/63.4bb3e179.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/64.23f955bc.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/65.77ffc240.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/66.a4503c94.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/67.bd3d6fa6.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/68.a003ac59.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/69.64c66f41.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/7.1e88af32.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/70.87dfbf7d.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/71.02806218.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/72.54e30335.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/73.002db3b4.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/74.e577570e.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/75.e610b3c9.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/76.f684a067.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/77.8be17062.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/78.cabc6873.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/79.83febd7c.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/8.91701106.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/80.19e3b7ca.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/81.1b4cafb3.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/82.752ef803.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/84.377a221f.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/85.016eaf98.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/86.604c5deb.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/87.d3a3662d.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/88.0cf7f581.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/89.40b002bd.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/9.94309dfb.js"><link rel="prefetch" href="/vuepress-theme-vdoing-doc/assets/js/90.086847f8.js">
    <link rel="stylesheet" href="/vuepress-theme-vdoing-doc/assets/css/0.styles.b0974950.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/vuepress-theme-vdoing-doc/" class="home-link router-link-active"><img src="/vuepress-theme-vdoing-doc/img/logo.png" alt="知识库" class="logo"> <span class="site-name can-hide">知识库</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/vuepress-theme-vdoing-doc/" class="nav-link">首页</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="指南" class="dropdown-title"><a href="/vuepress-theme-vdoing-doc/pages/a2f161/" class="link-title">指南</a> <span class="title" style="display:none;">指南</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/vuepress-theme-vdoing-doc/pages/52d5c3/" class="nav-link">主题初衷与诞生</a></li><li class="dropdown-item"><!----> <a href="/vuepress-theme-vdoing-doc/pages/a2f161/" class="nav-link">介绍</a></li><li class="dropdown-item"><!----> <a href="/vuepress-theme-vdoing-doc/pages/e08c97/" class="nav-link">模板集</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="案例" class="dropdown-title"><a href="/vuepress-theme-vdoing-doc/pages/5d571c/" class="link-title">案例</a> <span class="title" style="display:none;">案例</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/vuepress-theme-vdoing-doc/pages/5d571c/" class="nav-link">01.后端.md</a></li><li class="dropdown-item"><!----> <a href="/vuepress-theme-vdoing-doc/pages/4bd4c3/" class="nav-link">02.前端.md</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Docker" class="dropdown-title"><a href="/vuepress-theme-vdoing-doc/pages/68917f/" class="link-title">Docker</a> <span class="title" style="display:none;">Docker</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/vuepress-theme-vdoing-doc/pages/68917f/" class="nav-link">Docker安装与卸载</a></li><li class="dropdown-item"><!----> <a href="/vuepress-theme-vdoing-doc/pages/ceaf16/" class="nav-link">Docker常用命令</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Linux" class="dropdown-title"><a href="/vuepress-theme-vdoing-doc/pages/489582/" class="link-title">Linux</a> <span class="title" style="display:none;">Linux</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/vuepress-theme-vdoing-doc/pages/489582/" class="nav-link">01.防火墙设置.md</a></li><li class="dropdown-item"><!----> <a href="/vuepress-theme-vdoing-doc/pages/6babae/" class="nav-link">02.Linux文件压缩和解压要点.md</a></li><li class="dropdown-item"><!----> <a href="/vuepress-theme-vdoing-doc/pages/ccdc52/" class="nav-link">03.Linux 文件目录管理.md</a></li><li class="dropdown-item"><!----> <a href="/vuepress-theme-vdoing-doc/pages/503b98/" class="nav-link">04.Linux 文件内容查看编辑.md</a></li><li class="dropdown-item"><!----> <a href="/vuepress-theme-vdoing-doc/pages/e7b850/" class="nav-link">05.Linux 网络管理.md</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="空间聚合" class="dropdown-title"><a href="/vuepress-theme-vdoing-doc/pages/a3137a/" class="link-title">空间聚合</a> <span class="title" style="display:none;">空间聚合</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/vuepress-theme-vdoing-doc/pages/a3137a/" class="nav-link">OGC标准函数</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="后端" class="dropdown-title"><a href="/vuepress-theme-vdoing-doc/pages/fcb182/" class="link-title">后端</a> <span class="title" style="display:none;">后端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>MyBatis</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/vuepress-theme-vdoing-doc/pages/01e8a8/" class="nav-link">01.XML 映射器.md</a></li></ul></li><li class="dropdown-item"><h4>国产数据库</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/vuepress-theme-vdoing-doc/pages/3c4d09/" class="nav-link">01.达梦适配.md</a></li><li class="dropdown-subitem"><a href="/vuepress-theme-vdoing-doc/pages/3ab660/" class="nav-link">02.瀚高适配.md</a></li></ul></li><li class="dropdown-item"><h4>Selenium爬取数据</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/vuepress-theme-vdoing-doc/pages/e110c2/" class="nav-link">spring boot+selenium爬取数据.md</a></li></ul></li></ul></div></div> <a href="https://gitee.com/" target="_blank" rel="noopener noreferrer" class="repo-link">
    Source
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><!----> <nav class="nav-links"><div class="nav-item"><a href="/vuepress-theme-vdoing-doc/" class="nav-link">首页</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="指南" class="dropdown-title"><a href="/vuepress-theme-vdoing-doc/pages/a2f161/" class="link-title">指南</a> <span class="title" style="display:none;">指南</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/vuepress-theme-vdoing-doc/pages/52d5c3/" class="nav-link">主题初衷与诞生</a></li><li class="dropdown-item"><!----> <a href="/vuepress-theme-vdoing-doc/pages/a2f161/" class="nav-link">介绍</a></li><li class="dropdown-item"><!----> <a href="/vuepress-theme-vdoing-doc/pages/e08c97/" class="nav-link">模板集</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="案例" class="dropdown-title"><a href="/vuepress-theme-vdoing-doc/pages/5d571c/" class="link-title">案例</a> <span class="title" style="display:none;">案例</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/vuepress-theme-vdoing-doc/pages/5d571c/" class="nav-link">01.后端.md</a></li><li class="dropdown-item"><!----> <a href="/vuepress-theme-vdoing-doc/pages/4bd4c3/" class="nav-link">02.前端.md</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Docker" class="dropdown-title"><a href="/vuepress-theme-vdoing-doc/pages/68917f/" class="link-title">Docker</a> <span class="title" style="display:none;">Docker</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/vuepress-theme-vdoing-doc/pages/68917f/" class="nav-link">Docker安装与卸载</a></li><li class="dropdown-item"><!----> <a href="/vuepress-theme-vdoing-doc/pages/ceaf16/" class="nav-link">Docker常用命令</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Linux" class="dropdown-title"><a href="/vuepress-theme-vdoing-doc/pages/489582/" class="link-title">Linux</a> <span class="title" style="display:none;">Linux</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/vuepress-theme-vdoing-doc/pages/489582/" class="nav-link">01.防火墙设置.md</a></li><li class="dropdown-item"><!----> <a href="/vuepress-theme-vdoing-doc/pages/6babae/" class="nav-link">02.Linux文件压缩和解压要点.md</a></li><li class="dropdown-item"><!----> <a href="/vuepress-theme-vdoing-doc/pages/ccdc52/" class="nav-link">03.Linux 文件目录管理.md</a></li><li class="dropdown-item"><!----> <a href="/vuepress-theme-vdoing-doc/pages/503b98/" class="nav-link">04.Linux 文件内容查看编辑.md</a></li><li class="dropdown-item"><!----> <a href="/vuepress-theme-vdoing-doc/pages/e7b850/" class="nav-link">05.Linux 网络管理.md</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="空间聚合" class="dropdown-title"><a href="/vuepress-theme-vdoing-doc/pages/a3137a/" class="link-title">空间聚合</a> <span class="title" style="display:none;">空间聚合</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/vuepress-theme-vdoing-doc/pages/a3137a/" class="nav-link">OGC标准函数</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="后端" class="dropdown-title"><a href="/vuepress-theme-vdoing-doc/pages/fcb182/" class="link-title">后端</a> <span class="title" style="display:none;">后端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>MyBatis</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/vuepress-theme-vdoing-doc/pages/01e8a8/" class="nav-link">01.XML 映射器.md</a></li></ul></li><li class="dropdown-item"><h4>国产数据库</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/vuepress-theme-vdoing-doc/pages/3c4d09/" class="nav-link">01.达梦适配.md</a></li><li class="dropdown-subitem"><a href="/vuepress-theme-vdoing-doc/pages/3ab660/" class="nav-link">02.瀚高适配.md</a></li></ul></li><li class="dropdown-item"><h4>Selenium爬取数据</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/vuepress-theme-vdoing-doc/pages/e110c2/" class="nav-link">spring boot+selenium爬取数据.md</a></li></ul></li></ul></div></div> <a href="https://gitee.com/" target="_blank" rel="noopener noreferrer" class="repo-link">
    Source
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav> <div class="sidebar-slot sidebar-slot-top">
  <a href="http://apifox.cn/a103xugaoyi" target="_blank">
<!--    <img src="https://cdn.staticaly.com/gh/xugaoyi/blog-gitalk-comment@master/img/431669861564_.2470ykdcpbds.jpg" alt="npm" class="no-zoom" style="width: 100%;border-radius: 2px;">-->
<!--    <p style="text-align: center;margin: 0;color: #999;font-size: 12px;">(进入注册为作者充电)</p>-->
  </a></div> <ul class="sidebar-links"><li><a href="/vuepress-theme-vdoing-doc/pages/c9d188/" class="sidebar-link">链接分析</a></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>MyBatis</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/vuepress-theme-vdoing-doc/pages/01e8a8/" class="sidebar-link">XML 映射器</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>国产数据库</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/vuepress-theme-vdoing-doc/pages/3c4d09/" class="sidebar-link">PLUS达梦适配</a></li><li><a href="/vuepress-theme-vdoing-doc/pages/3ab660/" class="sidebar-link">PLUS瀚高适配</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>Selenium爬取数据</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/vuepress-theme-vdoing-doc/pages/e110c2/" class="sidebar-link">spring boot+selenium爬取数据</a></li></ul></section></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-06225672><div class="articleInfo" data-v-06225672><ul class="breadcrumbs" data-v-06225672><li data-v-06225672><a href="/vuepress-theme-vdoing-doc/" title="首页" class="iconfont icon-home router-link-active" data-v-06225672></a></li> <li data-v-06225672><span data-v-06225672>后端</span></li><li data-v-06225672><span data-v-06225672>待查阅</span></li></ul> <div class="info" data-v-06225672><div title="作者" class="author iconfont icon-touxiang" data-v-06225672><a href="https://gitee.com/wenlongjia" target="_blank" title="作者" class="beLink" data-v-06225672>Insist</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-06225672><a href="javascript:;" data-v-06225672>2023-06-06</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><!----> <h1><img src="">UDP协议<!----></h1> <div class="page-slot page-slot-top">
    <div class="wwads-cn wwads-horizontal page-wwads" data-id="136"></div>
    <style>
      .page-wwads{
        width:100%!important;
        min-height: 0;
        margin: 0;
      }
      .page-wwads .wwads-img img{
        width:80px!important;
      }
      .page-wwads .wwads-poweredby{
        width: 40px;
        position: absolute;
        right: 25px;
        bottom: 3px;
      }
      .wwads-content .wwads-text, .page-wwads .wwads-text{
        height: 100%;
        padding-top: 5px;
        display: block;
      }
  </style>
  </div> <div class="theme-vdoing-content content__default"><p>UDP数据协议是一种无连接的传输层协议，它不像TCP那样建立连接，而是直接向目标主机发送数据报文。在UDP传输过程中，数据报文可能会被丢失、重复、或乱序。</p> <p>一：UDP是如何接受数据报文的</p> <p>1.创建DatagramSocket对象，并指定端口号,这里监听的是8080端口，可根据自己的需求进行修改</p> <div class="language-text extra-class"><pre class="language-text"><code>DatagramSocket datagramSocket = new DatagramSocket(8080);
</code></pre></div><p>2.创建一个DatagramPacket对象，用于接收数据</p> <div class="language-text extra-class"><pre class="language-text"><code>byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
</code></pre></div><p>这里创建了一个长度为1024字节的字节数组，用于存放接收到的数据，然后使用DatagramPacket将该字节数组和数组的长度传递进去，表示UDP数据报文的最大长度。</p> <p>3.使用DatagramSocket的receive()方法接收数据</p> <div class="language-text extra-class"><pre class="language-text"><code>datagramSocket.receive(packet);
</code></pre></div><p>这里调用了DatagramSocket的<code>receive()</code>方法，该方法会一直阻塞，直到有数据到来。</p> <p>4.获取接收到的数据</p> <div class="language-text extra-class"><pre class="language-text"><code>byte[] receivedData = packet.getData();
</code></pre></div><p>这里通过<code>packet.getData()</code>方法获取接收到的数据。</p> <p>完整的代码示例demo：</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">UdpReceiver</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">try</span> <span class="token punctuation">(</span><span class="token class-name">DatagramSocket</span> datagramSocket <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">DatagramSocket</span><span class="token punctuation">(</span><span class="token number">8080</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> buffer <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token number">1024</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
            <span class="token class-name">DatagramPacket</span> packet <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">DatagramPacket</span><span class="token punctuation">(</span>buffer<span class="token punctuation">,</span> buffer<span class="token punctuation">.</span>length<span class="token punctuation">)</span><span class="token punctuation">;</span>
            
            <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                datagramSocket<span class="token punctuation">.</span><span class="token function">receive</span><span class="token punctuation">(</span>packet<span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> receivedData <span class="token operator">=</span> packet<span class="token punctuation">.</span><span class="token function">getData</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token class-name">String</span> receivedMsg <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span>receivedData<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span>packet<span class="token punctuation">.</span><span class="token function">getLength</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;Received message: &quot;</span> <span class="token operator">+</span> receivedMsg<span class="token punctuation">)</span><span class="token punctuation">;</span>
                
                <span class="token comment">// clear the buffer and packet for the next receive</span>
                <span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">fill</span><span class="token punctuation">(</span>buffer<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword">byte</span><span class="token punctuation">)</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                packet<span class="token punctuation">.</span><span class="token function">setLength</span><span class="token punctuation">(</span>buffer<span class="token punctuation">.</span>length<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>项目中实战demo:</p> <div class="language-text extra-class"><pre class="language-text"><code>
 /**
     * 接收UDP数据报文
     *
     * @param bufferSize 接收缓冲区大小
     * @param port       监听的端口号
     * @param headSpace  预留空间下标最大值
     * @throws Exception 接收失败时抛出异常
     */
    @Override
    public void receiveUdpData(int headSpace, int bufferSize, int port) {
        /*整体逻辑
        * 1.接受到数据报文
        * 2.获取到设备的编号，然后根据编号查看系统中是否有这个设备
        * 3.如果有这个设备，判断这个设备的数据协议是否是UDP协议并且设备是否是启用的
        * 4.如果符合条件，则根据不同的信息单元标识进行后续的数据处理，如果不符合逻辑则不处理
        * */
        // 启动UDP实时接收线程
        new Thread(() -&gt; {
            try {
                // 创建一个DatagramSocket对象，并指定监听的端口号
                DatagramSocket socket = new DatagramSocket(port);
                // 创建一个字节数组作为接收缓冲区
                byte[] buffer = new byte[bufferSize];
                // 创建一个DatagramPacket对象，用于接收数据
                DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
                // 实时接收数据报文
                while (true) {
                    // 接收数据
                    socket.receive(packet);
                    // 处理接收到的数据
                    byte[] message = packet.getData();
                    //获取信息单元标识
                    byte characteristic = message[headSpace + 2];
                    //工作状态报文、心跳报文、关机报文
                    Map&amp;lt;String, Object&gt; params = new HashMap&amp;lt;&gt;();
                    if (characteristic == 2 || characteristic == 3 || characteristic == 4) {
                    //逻辑代码
                    } else if (characteristic == 6) {
                       //逻辑代码
                    } else if (characteristic == 7) {
                       //逻辑代码
                    } else if (characteristic == 18) {
                       //逻辑代码
                    } else if (characteristic == 19) {
                       //逻辑代码
                    } else if (characteristic == 20) {
                      //逻辑代码
                    } else if (characteristic == 33) {
                      //逻辑代码
                    }

                    // 重置DatagramPacket对象，以便下次接收数据
                    packet.setData(buffer);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
    }
</code></pre></div><p>二：UDP是如何接受数据报文的</p> <p>完整的代码示例demo：</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">UdpSender</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">try</span> <span class="token punctuation">(</span><span class="token class-name">DatagramSocket</span> socket <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">DatagramSocket</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token class-name">String</span> message <span class="token operator">=</span> <span class="token string">&quot;Hello, world!&quot;</span><span class="token punctuation">;</span>
            <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> data <span class="token operator">=</span> message<span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">InetAddress</span> address <span class="token operator">=</span> <span class="token class-name">InetAddress</span><span class="token punctuation">.</span><span class="token function">getByName</span><span class="token punctuation">(</span><span class="token string">&quot;localhost&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">DatagramPacket</span> packet <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">DatagramPacket</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span> data<span class="token punctuation">.</span>length<span class="token punctuation">,</span> address<span class="token punctuation">,</span> <span class="token number">8080</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            socket<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span>packet<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;Sent message: &quot;</span> <span class="token operator">+</span> message<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>在此示例中，我们首先创建一个DatagramSocket对象，该对象用于发送UDP数据报文。然后我们定义要发送的消息作为字符串，并将其转换为字节数组。接着，我们使用InetAddress类获取本地主机的IP地址，并将其用于创建DatagramPacket对象。最后，我们使用DatagramSocket的send()方法将数据报文发送到指定的地址和端口号。</p> <p>请注意，在此示例中，我们使用localhost作为目标地址。如果您要向远程主机发送数据报文，则应指定远程主机的IP地址或域名。</p> <p>这是一个简单的示例，仅用于说明如何使用DatagramSocket类发送UDP数据报文。使用DatagramSocket发送UDP数据报文有很多细节需要考虑，例如包的最大长度，是否要设置超时时间等。</p> <p>项目中实战demo:</p> <div class="language-text extra-class"><pre class="language-text"><code>  /**
     * 发送UDP数据报文
     *
     * @param identification     信息单元标识ID
     * @param instructId         控制指令id
     * @param instructData_value 控制指令数据_指令值
     * @param instructData_param 控制指令数据_控制参数
     * @param host               目标主机地址
     * @param port               目标端口号
     * @throws Exception 发送失败时抛出异常
     */
    @Override
    public void sendUdpData(String identification, int instructId, int instructData_value, int instructData_param, String host, int port) {
        try {
            //发送的报文数组
            byte[] totleBytes = {};
            //预留存储空间大小,因为暂时不确定我们的报文前边是否还要发送其它数据
            byte[] headSpace = new byte[12];
            //推送的数据message
            byte[] message;
            // 时戳
            byte[] timestampBytes = TimestampUtil.getCurrentTimestampV2();

            switch (identification) {
                //1状态询问报文    5故障诊断请求报文  21控制指令报文
                case &quot;1&quot;:
                  //逻辑代码
                case &quot;5&quot;:
                  //逻辑代码
                case &quot;21&quot;:
                //逻辑代码
                default:

            }
            // 创建一个DatagramSocket对象
            DatagramSocket socket = new DatagramSocket();
            // 创建一个InetAddress对象，表示目标主机的地址
            InetAddress address = InetAddress.getByName(host);
            // 创建一个DatagramPacket对象，表示待发送的数据报文
            DatagramPacket packet = new DatagramPacket(totleBytes, totleBytes.length, address, port);
            // 发送数据报文
            socket.send(packet);
            // 关闭Socket对象
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
</code></pre></div><p>三：项目中用到的工具类</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">package</span> <span class="token namespace">com<span class="token punctuation">.</span>spdvt<span class="token punctuation">.</span>common<span class="token punctuation">.</span>utils</span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>nio<span class="token punctuation">.</span></span><span class="token class-name">ByteBuffer</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>time<span class="token punctuation">.</span></span><span class="token class-name">Duration</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>time<span class="token punctuation">.</span></span><span class="token class-name">Instant</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>time<span class="token punctuation">.</span></span><span class="token class-name">LocalDateTime</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>time<span class="token punctuation">.</span></span><span class="token class-name">ZoneId</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>time<span class="token punctuation">.</span>format<span class="token punctuation">.</span></span><span class="token class-name">DateTimeFormatter</span></span><span class="token punctuation">;</span>


<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TimestampUtil</span> <span class="token punctuation">{</span>

    <span class="token comment">/**
     * 获取当前时间的8字节时戳
     *
     * @return 8字节时戳的字节数组
     */</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token function">getCurrentTimestampV1</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 获取当前时间的时间戳（单位为毫秒）</span>
        <span class="token keyword">long</span> millis <span class="token operator">=</span> <span class="token class-name">Instant</span><span class="token punctuation">.</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toEpochMilli</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 计算从1970年1月1日0时0分0秒到现在的秒数</span>
        <span class="token keyword">long</span> seconds <span class="token operator">=</span> millis <span class="token operator">/</span> <span class="token number">1000</span><span class="token punctuation">;</span>

        <span class="token comment">// 计算当前毫秒值（除去秒数之后的部分）</span>
        <span class="token keyword">long</span> nanos <span class="token operator">=</span> <span class="token punctuation">(</span>millis <span class="token operator">%</span> <span class="token number">1000</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token number">1000000</span><span class="token punctuation">;</span>

        <span class="token comment">// 创建一个Duration对象，表示从1970年1月1日0时0分0秒到现在的时间间隔</span>
        <span class="token class-name">Duration</span> duration <span class="token operator">=</span> <span class="token class-name">Duration</span><span class="token punctuation">.</span><span class="token function">ofSeconds</span><span class="token punctuation">(</span>seconds<span class="token punctuation">,</span> nanos<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 将Duration对象转换为8字节的无符号整数</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> bytes <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">8</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            bytes<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">byte</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>duration<span class="token punctuation">.</span><span class="token function">toMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&gt;&gt;</span> <span class="token punctuation">(</span><span class="token number">56</span> <span class="token operator">-</span> i <span class="token operator">*</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token comment">// 返回生成的8字节时戳</span>
        <span class="token keyword">return</span> bytes<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">/**
     * 获取自纪元（1970 年 1 月 1 日）以来的当前时间（以毫秒为单位），Instant.now().toEpochMilli()并将其存储在millis变量中。
     * 除以1000计算秒数 ( seconds) 。millis
     * nanos通过对 1000 取模millis并将其乘以 1,000,000 来计算剩余的纳秒 ( )。
     * 使用 来创建Duration表示从纪元到当前时间的时间间隔的对象Duration.ofSeconds(seconds, nanos)。
     * 创建一个名为 的 8 字节数组bytes。
     * bytes通过遍历每个字节并相应地移动值中的位，用时间戳的二进制表示填充数组duration.toMillis()。
     * 返回生成的 8 字节时间戳。
     *
     * @return
     */</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token function">getCurrentTimestampV2</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">long</span> millis <span class="token operator">=</span> <span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">currentTimeMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> bytes <span class="token operator">=</span> <span class="token class-name">ByteBuffer</span><span class="token punctuation">.</span><span class="token function">allocate</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">putLong</span><span class="token punctuation">(</span>millis<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">array</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> bytes<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">/**
     * ByteBuffer.wrap(timestamp).getLong() 这行代码的作用是将一个字节数组（timestamp）转换为一个 long 类型的值。
     * 这里是如何实现的：
     * ByteBuffer.wrap(timestamp)：这个方法将输入的字节数组（timestamp）包装成一个 ByteBuffer 对象。
     * ByteBuffer 是 Java NIO（非阻塞 I/O）包中的一个类，它提供了一个方便的、可读可写的字节缓冲区。
     * .getLong()：ByteBuffer 对象上调用 getLong() 方法，从当前的缓冲区位置读取 8 个字节并将其合并为一个 long 类型的值。
     * 在调用 getLong() 之后，ByteBuffer 的位置将增加 8。这意味着，如果缓冲区中有多个连续的 long 值，你可以通过多次调用 getLong() 方法来逐个读取它们。
     * &lt;p&gt;
     * 接收一个字节数组（8字节长整型），将其转换回毫秒数，
     * 然后使用 Instant.ofEpochMilli() 方法创建一个 Instant 对象。
     * 接下来，它将 Instant 对象转换为一个 LocalDateTime 对象，
     * 使用系统默认时区
     *
     * @param timestamp
     * @return
     */</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">String</span> <span class="token function">parseTimestampToString</span><span class="token punctuation">(</span><span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> timestamp<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 将字节数组转换回长整型毫秒数</span>
        <span class="token keyword">long</span> millis <span class="token operator">=</span> <span class="token class-name">ByteBuffer</span><span class="token punctuation">.</span><span class="token function">wrap</span><span class="token punctuation">(</span>timestamp<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getLong</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 使用毫秒数创建一个 Instant 对象</span>
        <span class="token class-name">Instant</span> instant <span class="token operator">=</span> <span class="token class-name">Instant</span><span class="token punctuation">.</span><span class="token function">ofEpochMilli</span><span class="token punctuation">(</span>millis<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 使用系统默认时区将 Instant 转换为 LocalDateTime</span>
        <span class="token class-name">LocalDateTime</span> dateTime <span class="token operator">=</span> <span class="token class-name">LocalDateTime</span><span class="token punctuation">.</span><span class="token function">ofInstant</span><span class="token punctuation">(</span>instant<span class="token punctuation">,</span> <span class="token class-name">ZoneId</span><span class="token punctuation">.</span><span class="token function">systemDefault</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 创建一个 DateTimeFormatter 以格式化日期时间</span>
        <span class="token class-name">DateTimeFormatter</span> formatter <span class="token operator">=</span> <span class="token class-name">DateTimeFormatter</span><span class="token punctuation">.</span><span class="token function">ofPattern</span><span class="token punctuation">(</span><span class="token string">&quot;yyyy-MM-dd HH:mm:ss&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 使用 formatter 将 LocalDateTime 对象格式化为字符串</span>
        <span class="token class-name">String</span> dateTimeString <span class="token operator">=</span> dateTime<span class="token punctuation">.</span><span class="token function">format</span><span class="token punctuation">(</span>formatter<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">return</span> dateTimeString<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>



    <span class="token comment">//int转byte[]</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token function">intToByteArray</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> bytes <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
        bytes<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">byte</span><span class="token punctuation">)</span> i<span class="token punctuation">;</span>
        bytes<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">byte</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>i <span class="token operator">&gt;&gt;</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        bytes<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">byte</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>i <span class="token operator">&gt;&gt;</span> <span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        bytes<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">byte</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>i <span class="token operator">&gt;&gt;</span> <span class="token number">24</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> bytes<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">//byte[]转int</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">int</span> <span class="token function">byteArrayToInt</span><span class="token punctuation">(</span><span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> bytes<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">int</span> res <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> bytes<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            res <span class="token operator">+=</span> <span class="token punctuation">(</span>bytes<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">&amp;</span> <span class="token number">0xff</span><span class="token punctuation">)</span> <span class="token operator">&lt;&lt;</span> i <span class="token operator">*</span> <span class="token number">8</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">return</span> res<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>


<span class="token punctuation">}</span>

</code></pre></div></div></div>  <div class="page-edit"><!----> <!----> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2023/08/14, 13:28:01</span></div></div> <div class="page-nav-wapper"><!----> <!----></div></div> <!----></main></div> <div class="footer"><div class="icons"></div> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2023-2024
    <span>Insist | MIT License</span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
          跟随系统
        </li><li class="iconfont icon-rijianmoshi">
          浅色模式
        </li><li class="iconfont icon-yejianmoshi">
          深色模式
        </li><li class="iconfont icon-yuedu">
          阅读模式
        </li></ul></div></div> <!----> <!----> <!----></div><div class="global-ui"></div></div>
    <script src="/vuepress-theme-vdoing-doc/assets/js/app.f061d58b.js" defer></script><script src="/vuepress-theme-vdoing-doc/assets/js/2.9bf30e88.js" defer></script><script src="/vuepress-theme-vdoing-doc/assets/js/83.460cd49b.js" defer></script>
  </body>
</html>
